package com.hqyj.controller;

import com.hqyj.pojo.DataMessage;
import com.hqyj.pojo.UserInfo;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController

@SessionAttributes("admin")
public class AjaxController {

    //处理student的增删改查的ajax请求
    @RequestMapping("/stuAjax/{url}")
    public DataMessage stuAjax(@PathVariable("url")String url, UserInfo userInfo){
        DataMessage dataMessage = new DataMessage();
        if (url.equals("add")){
            System.out.println("新增的业务逻辑");
            dataMessage.setInfo("新增的业务逻辑");
            dataMessage.setSuccess(true);
            //新增的业务逻辑
        }else if (url.equals("update")){
            System.out.println("修改的业务逻辑");
            dataMessage.setInfo("修改的业务逻辑");
            dataMessage.setSuccess(true);
            //修改的业务逻辑
        }else{
            System.out.println("查询的业务逻辑");
            List<DataMessage> list = new ArrayList<>();
            dataMessage.setData(list);
            //查询的业务逻辑
        }
        return dataMessage;
    }
    //处理登录的get和post请求
    @RequestMapping("/loginAjax/{url}")
    private DataMessage loginAjax(@PathVariable("url")String url, UserInfo userInfo, ModelMap map){
        DataMessage data = new DataMessage();
        System.out.println(userInfo);
        if (userInfo.getUserName().equals("admin")&&userInfo.getUserPwd().equals("123")) {
            System.out.println("登陆成功");
            //session.setAttribute("admin",userInfo.getUserName());
            map.addAttribute("admin",userInfo.getUserName());
            data.setSuccess(true);
            data.setInfo("登陆成功");
        }else {
            System.out.println("登陆失败");
            data.setSuccess(false);
            data.setInfo("登陆失败");
        }
        return data;
    }
}
